package com.example.demo;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.demo.databinding.ActivityBaseBinding;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class BaseActivity extends AppCompatActivity {
    private Handler handler = new Handler(Looper.getMainLooper());
    private Runnable updateTimeRunnable = this::updateTime;
    protected TextView tvBottomTime;
    private ActivityBaseBinding baseBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //设置屏幕横向
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        // 初始化基础布局
        baseBinding = ActivityBaseBinding.inflate(getLayoutInflater());
        super.setContentView(baseBinding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.base), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        super.onCreate(savedInstanceState);
    }
    @Override
    public void setContentView(int layoutResID) {
        // 1. 确保基础布局已初始化
        if (baseBinding == null) {
            baseBinding = ActivityBaseBinding.inflate(getLayoutInflater());
        }

        // 2. 清空容器并添加新视图
        FrameLayout container = baseBinding.container;
        container.removeAllViews();
        View childView = getLayoutInflater().inflate(layoutResID, container, false);
        container.addView(childView);

        // 3. 设置最终布局
        super.setContentView(baseBinding.getRoot());

        // 4. 初始化时间并开始更新
        tvBottomTime = baseBinding.tvBottomTime;
        updateTime();
    }

    public void setContentView(View view) {
        // 1. 确保基础布局已初始化
        if (baseBinding == null) {
            baseBinding = ActivityBaseBinding.inflate(getLayoutInflater());
        }

        // 2. 清空容器并添加新视图
        FrameLayout container = baseBinding.container;
        container.removeAllViews();
        container.addView(view);

        // 3. 设置最终布局
        super.setContentView(baseBinding.getRoot());

        // 4. 初始化时间并开始更新
        tvBottomTime = baseBinding.tvBottomTime;
        updateTime();
    }

    private void updateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String currentTime = sdf.format(new Date());
        if (tvBottomTime != null) {
            tvBottomTime.setText(currentTime);
        }

        // 每秒更新一次
        handler.postDelayed(updateTimeRunnable, 1000);
    }

    @Override
    protected void onDestroy() {
        handler.removeCallbacks(updateTimeRunnable);
        super.onDestroy();
    }
}